package cn.mianshiyi.example.demo;


import cn.mianshiyi.localcache.client.etcd.EtcdAbstractLocalCache;
import cn.mianshiyi.localcache.client.etcd.EtcdCacheConfig;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shangqing.liu
 */
//@Service
public class EtcdTestCache extends EtcdAbstractLocalCache<String, String> {

    @PostConstruct
    public void init() {
        String[] addr = new String[]{"http://127.0.0.1:2379"};
        EtcdCacheConfig config = new EtcdCacheConfig(10, 100, 60 * 60
                , "/test1/etcdPath", addr);
        super.init(config);
    }

    @Override
    public String refresh(String key) {
        return UUID.randomUUID().toString();
    }

    @Override
    public void receive(byte[] data) {
        String s = new String(data
        );
        System.out.println("接收到数据"+s);
    }

    public static void main(String[] args) throws InterruptedException {
        String[] addr = new String[]{"http://127.0.0.1:2379"};
        EtcdCacheConfig config = new EtcdCacheConfig(10, 100, 60 * 60
                , "/test1/etcdPath", addr);
        EtcdTestCache etcdTestCache = new EtcdTestCache();
        etcdTestCache.init(config);
        AtomicInteger atomicInteger = new AtomicInteger(10);
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    etcdTestCache.broadcast(String.valueOf(atomicInteger.getAndAdd(10)).getBytes(StandardCharsets.UTF_8));

                }
            }).start();
        }
        Thread.sleep(100000);
    }

}
